package site.param.service.parameter.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.dao.parameter.mapper.ExtraParamMysqlMapper;
import site.dao.parameter.mapper.ParamMysqlMapper;
import site.dao.parameter.model.ParamMysql;
import site.dao.parameter.model.ParamMysqlExample;
import site.param.init.ParameterMap;
import site.param.init.ParameterType;
import site.param.service.parameter.IParameterService;
import site.param.util.ZookeeperUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2017/1/3.
 */
@Service
public class ParameterService implements IParameterService {
    @Autowired
    private ParamMysqlMapper paramMysqlMapper;
    @Autowired
    private ExtraParamMysqlMapper extraParamMysqlMapper;

    @Override
    public Map<String, ParameterMap> initParamter(Map<String, ParameterMap> parameterMapMap) {
        return paramLoadAndSave(parameterMapMap);
    }

    @Override
    public ParameterMap reloadParamter(ParameterMap parameterMap) {
        if (parameterMap==null||parameterMap.getParameterTypes()==null){
            return  null;
        }
        ParamMysqlExample example=new ParamMysqlExample();
        example.createCriteria().andSerialVersionUidEqualTo(parameterMap.getSerialUID());
        List<ParamMysql> paramMysqlList=paramMysqlMapper.selectByExample(example);
        for (ParamMysql paramMysql :paramMysqlList){
            if (parameterMap.getParameterTypes().get(paramMysql.getFiledName())!=null){
                parameterMap.getParameterTypes().get(paramMysql.getFiledName()).setFiledValue(paramMysql.getFiledValue());
            }
        }
        return parameterMap;
    }


    private Map<String, ParameterMap> paramLoadAndSave(Map<String, ParameterMap> parameterMapMap){
        List<String> serialVersionUidList=new ArrayList<>();
        Map<String,ParamMysql> baseParamTypeMap=new HashMap<>();
        ParamMysql originalParamMysql=null;
        ParameterMap parameterMap=null;
        Map<String, ParameterType> parameterTypes=null;
        Date now=new Date();
         Set<String> sets=new HashSet<>();

        try {
            for (String sUid: parameterMapMap.keySet()){
                sets.add(ZookeeperUtil3.INSTANCE.zkRootNode()+"/"+sUid);
                serialVersionUidList.add(sUid);
                parameterMap=parameterMapMap.get(sUid);
                parameterTypes=parameterMap.getParameterTypes();
                ParameterType parameterType=null;
                for (String str: parameterTypes.keySet()){
                    parameterType=parameterTypes.get(str);
                    originalParamMysql=new ParamMysql();
                    originalParamMysql.setFiledValue(parameterType.getFiledValue());
                    originalParamMysql.setFiledName(parameterType.getFiledName());
                    originalParamMysql.setObjUnique(sUid+str);
                    originalParamMysql.setObjName(parameterMap.getName());
                    originalParamMysql.setObjDesc(parameterMap.getParameterDesc());
                    originalParamMysql.setSerialVersionUid(sUid);
                    originalParamMysql.setCreateTime(now);
                    originalParamMysql.setPackageName(parameterMap.getPackageName());

                    baseParamTypeMap.put(originalParamMysql.getObjUnique(),originalParamMysql);
                }
            }
            ZookeeperUtil.INSTANCE.createNode(sets);
            ParamMysqlExample example=new ParamMysqlExample();
            example.createCriteria().andSerialVersionUidIn(serialVersionUidList);
            List<ParamMysql> paramMysqlList=paramMysqlMapper.selectByExample(example);
            for (ParamMysql paramMysql :paramMysqlList){
                if(parameterMapMap.get(paramMysql.getSerialVersionUid())!=null&&parameterMapMap.get(paramMysql.getSerialVersionUid()).getParameterTypes()!=null){
                    if(paramMysql.getFiledName()!=null){
                        parameterMapMap.get(paramMysql.getSerialVersionUid()).getParameterTypes().get(paramMysql.getFiledName()).setFiledValue(paramMysql.getFiledValue());
                    }
                }
                baseParamTypeMap.remove(paramMysql.getObjUnique());
            }
            insertBatch(new ArrayList<ParamMysql>(baseParamTypeMap.values()));
        } catch (Exception e) {
            //log
        }
        return parameterMapMap;

    }

    private void insertBatch(List<ParamMysql> paramMysqls){
        if (paramMysqls!=null){
          //  extraParamMysqlMapper.insertBatch(paramMysqls);
            for (ParamMysql paramMysql:paramMysqls){
                paramMysqlMapper.insertSelective(paramMysql);
            }
        }
    }


    @Override
    public boolean updateParamter(ParamMysql paramMysql) {
        if (paramMysql!=null){
            paramMysql.setUpdateTime(new Date());
        }
        boolean flag= paramMysqlMapper.updateByPrimaryKeySelective(paramMysql)>0?true:false;
        String node= "/"+paramMysql.getSerialVersionUid();
        if (flag){
           return ZookeeperUtil.INSTANCE.updateNode(node);
        }
        return false;
    }
    @Override
    public List<String> getPackageNames(){
        return extraParamMysqlMapper.getPackageNames();
    }

    @Override
    public List<Map<String,String>> getClassByPackage(String packageName){
        List<Map<String,String>> paramMysqlList= extraParamMysqlMapper.getClassNames(packageName);

        return paramMysqlList;
    }


    @Override
    public List<ParamMysql> getFiledValues(String serialVersionUid){
        if (serialVersionUid==null||serialVersionUid.equals("-1")){
            return null;
        }
        ParamMysqlExample example=new ParamMysqlExample();
        example.createCriteria().andSerialVersionUidEqualTo(serialVersionUid);
        List<ParamMysql> paramMysqlList= paramMysqlMapper.selectByExample(example);
        return paramMysqlList;
    }

    @Override
    public List<ParamMysql> search(String keyValue){
        if (keyValue!=null){
            return extraParamMysqlMapper.search(keyValue);
        }
        return null;
    }
}
